#coding:utf-8
import re,os, sys
sys.path.append("matchutils")
from utils.utils import *
from algo.parse_name import *
from utils.pinyin import *
from utils.jieba_wrapper import *
from utils.wordtool import *

gpy = PinYin()
gpy.load_word()

"""
数字要格外小心处理
版本号相同
之版本相同
之和冒号
草莓乐园之快乐生活	==> delflag = 1
草莓乐园之幸福时光

"""

alias_names = {}
f = open(dict_dir + '/ent_map.txt', 'rb')
for line in f:
    line = line.strip()
    if line == '': continue
    line = line.lower()
    fields = line.split('\t')
    if len(fields) != 2: continue
    w1, w2 = fields
    w1 = gpa._convdigit( w1 )
    w2 = gpa._convdigit( w2 )

    if w1 not in alias_names:
        alias_names[w1] = []
    alias_names[w1].append(w2)

    if w2 not in alias_names:
        alias_names[w2] = []
    alias_names[w2].append(w1)
f.close()

category_words = {}
category_words['微电影'] = 1
category_words['微影院'] = 1
category_words['短片'] = 1
category_words['记录片'] = 1

care_part_words = {'上':1, '中':1, '下':1 , '上部':1, '中部':1, '下部':1, '上篇':1 , '中篇':1, '下篇':1, '上半部':1, '下半部':1 }

diff_part_words = {'第2部':1, '第3部':1, '第4部':1, '第5部':1, '第6部':1, '第7部':1, '第8部':1, '第9部':1, '第10部':1}
diff_part_words['第2季'] = 1
diff_part_words['第3季'] = 1
diff_part_words['第4季'] = 1
diff_part_words['第5季'] = 1
diff_part_words['第6季'] = 1
diff_part_words['第7季'] = 1
diff_part_words['第8季'] = 1
diff_part_words['第9季'] = 1
diff_part_words['第10季'] = 1
diff_part_words['第11季'] = 1
diff_part_words['第12季'] = 1
diff_part_words['第2篇'] = 1
diff_part_words['第3篇'] = 1
diff_part_words['第4篇'] = 1
diff_part_words['第5篇'] = 1
diff_part_words['第6篇'] = 1
diff_part_words['第7篇'] = 1
diff_part_words['第8篇'] = 1
diff_part_words['第9篇'] = 1
diff_part_words['第10篇'] = 1
diff_part_words['第11篇'] = 1
diff_part_words['第12篇'] = 1
diff_part_words['第2话'] = 1
diff_part_words['第3话'] = 1
diff_part_words['第4话'] = 1
diff_part_words['第5话'] = 1
diff_part_words['第6话'] = 1
diff_part_words['第7话'] = 1
diff_part_words['第8话'] = 1
diff_part_words['第9话'] = 1
diff_part_words['第10话'] = 1
diff_part_words['第11话'] = 1
diff_part_words['第12话'] = 1
diff_part_words['第2辑'] = 1
diff_part_words['第3辑'] = 1
diff_part_words['第4辑'] = 1
diff_part_words['第5辑'] = 1
diff_part_words['第6辑'] = 1
diff_part_words['第7辑'] = 1
diff_part_words['第8辑'] = 1
diff_part_words['第9辑'] = 1
diff_part_words['第10辑'] = 1
diff_part_words['第11辑'] = 1
diff_part_words['第12辑'] = 1

diff_unit_words = {}
diff_unit_words['unit1'] = 1
diff_unit_words['unit2'] = 1
diff_unit_words['unit3'] = 1
diff_unit_words['unit4'] = 1
diff_unit_words['unit5'] = 1
diff_unit_words['unit6'] = 1
diff_unit_words['unit7'] = 1
diff_unit_words['unit8'] = 1
diff_unit_words['unit9'] = 1
diff_unit_words['unit10'] = 1
diff_unit_words['unit11'] = 1
diff_unit_words['unit12'] = 1
diff_unit_words['unit13'] = 1
diff_unit_words['unit14'] = 1
diff_unit_words['unit15'] = 1
diff_unit_words['unit16'] = 1
diff_unit_words['unit17'] = 1
diff_unit_words['unit18'] = 1
diff_unit_words['unit19'] = 1
diff_unit_words['unit20'] = 1
diff_unit_words['unit20'] = 1
diff_unit_words['part1'] = 1
diff_unit_words['part2'] = 1
diff_unit_words['part3'] = 1
diff_unit_words['part4'] = 1
diff_unit_words['part5'] = 1
diff_unit_words['part6'] = 1
diff_unit_words['part7'] = 1
diff_unit_words['part8'] = 1
diff_unit_words['part9'] = 1
diff_unit_words['part10'] = 1
diff_unit_words['part11'] = 1
diff_unit_words['part12'] = 1
diff_unit_words['part13'] = 1
diff_unit_words['part14'] = 1
diff_unit_words['part15'] = 1
diff_unit_words['part16'] = 1
diff_unit_words['part17'] = 1
diff_unit_words['part18'] = 1
diff_unit_words['part19'] = 1
diff_unit_words['part20'] = 1
diff_unit_words['part21'] = 1
diff_unit_words['part22'] = 1
diff_unit_words['part23'] = 1
diff_unit_words['part24'] = 1
diff_unit_words['parta'] = 1
diff_unit_words['partb'] = 1
diff_unit_words['partc'] = 1
diff_unit_words['partd'] = 1
diff_unit_words['module1'] = 1
diff_unit_words['module2'] = 1
diff_unit_words['module3'] = 1
diff_unit_words['module4'] = 1
diff_unit_words['module5'] = 1
diff_unit_words['module6'] = 1
diff_unit_words['module7'] = 1
diff_unit_words['module8'] = 1
diff_unit_words['module9'] = 1
diff_unit_words['module10'] = 1
diff_unit_words['module11'] = 1
diff_unit_words['module12'] = 1
diff_unit_words['module13'] = 1
diff_unit_words['module14'] = 1
diff_unit_words['module15'] = 1
diff_unit_words['module16'] = 1
diff_unit_words['module17'] = 1
diff_unit_words['module18'] = 1
diff_unit_words['module19'] = 1
diff_unit_words['module20'] = 1


bad_alias_names = {}
bad_alias_names['因为爱情有奇缘'] = 1
bad_alias_names['因为爱情有奇迹'] = 1
bad_alias_names['因为爱情有多美'] = 1
bad_alias_names['因为爱情有晴天'] = 1
bad_alias_names['加油爱人'] = 1
bad_alias_names['幸福爱人'] = 1
bad_alias_names['幸福三部曲'] = 1
bad_alias_names['幸福两部曲'] = 1
bad_alias_names['幸福爱人'] = 1
bad_alias_names['小菊的春天'] = 1
bad_alias_names['小菊的秋天'] = 1

class VideoNameSim(object):
    album = None
    video = None
    stopwords = {}
    stopwords['之'] = 0.01
    stopwords['的'] = 0.01
    stopwords['系列'] = 0.02
    stopwords['完结'] = 0.01
    stopwords['已完结'] = 0.01
    stopwords['未完结'] = 0.01
    stopwords['全'] = 0.01
    stopwords['就'] = 0.01
    stopwords['热播'] = 0.1
    stopwords['经典'] = 0.1
    stopwords['更新中'] = 0.01
    stopwords['更新完毕'] = 0.01
    stopwords['の'] = 0.01
    stopwords['很'] = 0.1
    stopwords['又'] = 0.1
    stopwords['最'] = 0.1
    stopwords['啊'] = 0.1
    stopwords['呢'] = 0.1
    stopwords['吧'] = 0.1
    stopwords['呀'] = 0.1
    stopwords['啦'] = 0.3
    stopwords['么'] = 0.1
    stopwords['吗'] = 0.1
    stopwords['得'] = 0.5
    stopwords['着'] = 0.4
    stopwords['也'] = 0.3
    stopwords['呗'] = 0.3
    stopwords['嘛'] = 0.3
    stopwords['却'] = 0.7
    stopwords['是'] = 0.5
    stopwords['as'] = 0.5
    stopwords['and'] = 0.5
    stopwords['the'] = 0.2
    stopwords['an'] = 0.5
    stopwords['系列片'] = 0.1
    stopwords['越剧'] = 0.5

    stopwords['.'] = 0
    stopwords['<'] = 0
    stopwords['['] = 0
    stopwords[']'] = 0
    stopwords['；'] = 0
    stopwords['。'] = 0
    stopwords['；'] = 0
    stopwords['，'] = 0
    stopwords['《'] = 0
    stopwords['》'] = 0
    stopwords['【'] = 0
    stopwords['】'] = 0
    stopwords['（'] = 0
    stopwords['）'] = 0
    stopwords['：'] = 0
    stopwords[':'] = 0
    stopwords[']'] = 0
    stopwords['['] = 0
    stopwords['#'] = 0
    stopwords['@'] = 0
    stopwords['!'] = 0
    stopwords[','] = 0
    stopwords['-'] = 0
    stopwords['+'] = 0
    stopwords['<'] = 0
    stopwords['>'] = 0
    stopwords['?'] = 0
    stopwords[';'] = 0
    stopwords['剧'] = 0.01
    stopwords['版'] = 0.01
    stopwords['年'] = 0.01
    stopwords['片'] = 0.01
    stopwords['了'] = 0.01

    #synwords = {}

    _romawords = {}
    _romawords['Ⅰ'] = '1'
    _romawords['Ⅱ'] = '2'
    _romawords['Ⅲ'] = '3'
    _romawords['Ⅳ'] = '4'
    _romawords['Ⅴ'] = '5'
    _romawords['Ⅵ'] = '6'
    _romawords['Ⅶ'] = '7'
    _romawords['Ⅷ'] = '8'
    _romawords['Ⅸ'] = '9'
    _romawords['Ⅹ'] = '10'
    _romawords['Ⅺ'] = '11'
    _romawords['Ⅻ'] = '12'
    _romawords['i'] = '1'
    _romawords['ii'] = '2'
    _romawords['iii'] = '3'
    _romawords['iv'] = '4'
    _romawords['v'] = '5'
    _romawords['vi'] = '6'
    _romawords['vii'] = '7'
    _romawords['viii'] = '8'
    _romawords['ix'] = '9'
    _romawords['x'] = '10'
    _romawords['xi'] = '11'
    _romawords['xii'] = '12'
    _romawords['ⅰ'] = '1'
    _romawords['ⅱ'] = '2'
    _romawords['ⅲ'] = '3'
    _romawords['ⅳ'] = '4'
    _romawords['ⅴ'] = '5'
    _romawords['ⅵ'] = '6'
    _romawords['ⅶ'] = '7'
    _romawords['ⅷ'] = '8'
    _romawords['ⅸ'] = '9'
    _romawords['ⅹ'] = '10'
    _romawords['ⅺ'] = '11'
    _romawords['ⅻ'] = '12'

    part_pattern_f = re.compile(u'第[0-9零一二三四五六七八九十]+[季部辑话]')
    part_pattern = re.compile(u'第([0-9零一二三四五六七八九十]+)[季部辑话]')

    ori_pattrn_del = re.compile(u'【（\[\([]原版[）】\)\]]')

    dig_pattern = re.compile(u'[零一二三四五六七八九十百千]+')
    utf8_order_pattern = re.compile('第([0-9]+)')
    eword_pattern = re.compile(u'[a-z0-9]+')
    uni_part_pattern = re.compile(u'^第([0-9]+)[期届部季辑篇节章回话]$')
    uni_updn_pattern = re.compile(u'^([上中下])[部篇]$')
    updn_pattern2 = re.compile(u'([上中下])[部篇]')
    bri_updwn_pattrn = re.compile(u'[\[【（][ ]?([上中下])[ ]?[】）\)]')
            
    _coll_ups = {'上':1, '上部':1, '上篇':1, '前记':1, '前传':1}
    _coll_dns = {'下':1, '下部':1, '下篇':1, '后记':1, '后传':1}
    huaxus = {'花絮':1, '预告':1, '预告片':1, '片花':1}

    _catwords = {'动画版':1,'动画片':1, '动漫版':1, '漫画版':1}

    _cwordimps = {}
    _cwordimps['纪录片'] = 1.2
    _cwordimps['微电影'] = 1.2
    _cwordimps['微影院'] = 1.2
    _cwordimps['集'] = 0.05
    _cwordimps['第'] = 0.05
    _cwordimps['部'] = 0.05
    _cwordimps['季'] = 0.05
    _cwordimps['与'] = 0.05
    _cwordimps['和'] = 0.05
    _cwordimps['最'] = 0.05
    _cwordimps['和'] = 0.05
    _cwordimps['很'] = 0.05
    _cwordimps['年版'] = 0.05
    _cwordimps['无字幕版'] = 0.05
    _cwordimps['中字'] = 0.05
    _cwordimps['免费版'] = 0.1
    _cwordimps['3d'] = 0.3
    _cwordimps['清晰版'] = 0.05
    _cwordimps['超长版'] = 0.1
    _cwordimps['special'] = 1.3
    _cwordimps['final'] = 0.05
    _cwordimps['sp'] = 1.3
    _cwordimps['毕业季'] = 1.2
    _cwordimps['圣诞季'] = 1.2
    _cwordimps['演唱季'] = 1.2
    _cwordimps['男神季'] = 1.2
    _cwordimps['女神季'] = 1.2
    _cwordimps['女人季'] = 1.2
    _cwordimps['电影'] = 0.4
    _cwordimps['电视剧'] = 0.4
    _cwordimps['动漫'] = 0.4
    _cwordimps['动画'] = 0.3
    _cwordimps['综艺'] = 0.3
    _cwordimps['越剧'] = 0.4
    _cwordimps['京剧'] = 0.5
    _cwordimps['美剧'] = 0.5
    _cwordimps['韩剧'] = 0.5
    _cwordimps['日剧'] = 0.6
    _cwordimps['连续剧'] = 0.4
    _cwordimps['国产剧'] = 0.4
    _cwordimps['影院'] = 0.3
    _cwordimps['终极版'] = 0.3
    _cwordimps['大'] = 0.2
    _cwordimps['小'] = 0.2
    _cwordimps['新版'] = 0.2
    _cwordimps['新'] = 0.2
    _cwordimps['老版'] = 0.2
    _cwordimps['旧版'] = 0.2
    _cwordimps['完整版'] = 0.1
    _cwordimps['高清版'] = 0.15
    _cwordimps['重版'] = 0.1
    _cwordimps['韩语版'] = 0.1
    _cwordimps['国语'] = 0.05
    _cwordimps['国语版'] = 0.05
    _cwordimps['英文版'] = 0.1
    _cwordimps['粤语版'] = 0.1
    _cwordimps['破解版'] = 0.1
    _cwordimps['标准版'] = 0.1
    _cwordimps['外传'] = 1.5
    _cwordimps['绘画'] = 1.5
    _cwordimps['记忆'] = 1.5
    _cwordimps['美术'] = 1.5
    _cwordimps['音乐'] = 1.5
    _cwordimps['识字'] = 1.5
    _cwordimps['记忆篇'] = 1.5
    _cwordimps['音乐篇'] = 1.5
    _cwordimps['美术篇'] = 1.5
    _cwordimps['绘画篇'] = 1.5
    _cwordimps['识字篇'] = 1.5
    _cwordimps['文明篇'] = 1.5
    _cwordimps['战争篇'] = 1.5
    _cwordimps['经济篇'] = 1.5
    _cwordimps['科技篇'] = 1.5
    _cwordimps['政治篇'] = 1.5
    _cwordimps['时事篇'] = 1.5
    _cwordimps['儿歌'] = 1.5
    _cwordimps['儿童故事'] = 1.5
    _cwordimps['舞蹈'] = 1.5
    _cwordimps['童趣'] = 1.5
    _cwordimps['古诗'] = 1.5
    _cwordimps['盛典'] = 1.5

    #最好换成idf

    #_cwordimps['网络完整版'] = 0.01
    #_cwordimps['未删减版'] = 0.01

    pth_lang = {'国语':1,'汉语':1, '普通话':1, '中文':1, '中文版':1}
    mny_lang = {'闽南语':1, '闽南话':1, '台语':1}
    gdh_lang = {'广东话':1, '粤语':1 }

    def __init__( self, _album, _video ):
        self.album = _album
        self.video = _video
        self._xlwords = []
        self._ylwords = []
        self._xTitle = ''
        self._yTitle = ''
        self._delflag = 0
        self._ypart = []
        self._partok = 0
        self._cateword = 0
        self.bPartFlag = 0
        self._shortMovie = False
        self._diffparts = []
        self._title_year_flag = 0

    def get_title_year_match(self):
        return self._title_year_flag

    def get_cateword_flag(self):
        return self._cateword

    def is_ShortMovie(self):
        return self._shortMovie

    def get_vide_type_name(self):
        vt = self.album.get('video_type', None )
        if vt == 1:
            return '电视剧'
        if vt == 2:
            return '电影'
        if vt == 3:
            return '动漫'
        if vt == 4:
            return '综艺'
        if vt == 16:
            return '记录片'
        return None

    def _dealTitle(self, namestr ):
        result = []
        namestr = namestr.decode('utf-8', 'ignore')
        r = self.part_pattern.search( namestr )
        if r:
            src = r.group() 
            dst = r.groups()[0]
            dst = fromHanDigit(dst) 
            if isinstance(dst, str):
                dst = dst.decode('utf-8', 'ignore')
            namestr = namestr.replace(src, dst, 1 )

        namestr = namestr.encode('utf-8', 'ignore')
        words = cutwords( namestr )
        for item in words:
            word = item['word'].strip()
            flag = item['flag']
            if word == '': continue
            print word.decode('utf-8', 'ignore').encode('gbk', 'ignore')
            if word in self._romawords:
                word = self._romawords[ word ]
                result.append( word )
                continue
            if word.isalnum():
                result.append( word )
                continue
            if word == '之' or word == '系列' or word == '完结' or word == '的':
                continue
            if flag == 'w' or flag == 'x':
                continue
            result.append( word )

        result = ' '.join(result)

        return result

    def cleanString(self,line):
        if not isinstance(line, str) and not isinstance(line, unicode):
            return ''
        if isinstance(line, str):
            line = line.decode('utf-8', 'ignore')
        line = line.replace(u'(完结)', u'').replace(u'（完结）',u'')

        #数字转换
        results = self.dig_pattern.findall( line )
        #results = re.findall(u'[一二三四五六七八九十百千]+', line )
        for ori in results:
            dst = fromHanDigit( ori)
            dst = dst.decode('utf-8', 'ignore')
            if ori == u'零零七':
                dst = u'007'
            line = line.replace(ori, dst, 1)

        words = []
        for ch in line:
            if is_unicode_hanzi(ch):
                words.append(ch)         
                continue
            if ch.isalnum():
                words.append(ch)         
                continue
        line =  ''.join(words)
        return line

    def get_valid_words(self, words ):
        l = []
        if not words :
            return []
        for word in words:
            flag = word['flag']
            w = word['word'].strip()
            if w =='': continue
            #日本字符
            if flag == 'x' and is_japanese(w):
                l.append( w )
                continue
            #标点
            if flag == 'w' or (flag == "x" and not w.isalnum() ): continue
            #听用词
            if w in self.stopwords and self.stopwords[w] <= 0.001: 
               continue
            #罗马数字
            if w in self._romawords:
                w = self._romawords[w]
            l.append( w )
        return l

    def _getFieldWords(self, item ):
        hashwords = {}
        keys = ['director_name', 'actor_name', 'area_name', 'language_name', 'screen_time', 'category_name', 'name_fr', 'sub_title']
        for key in keys:
             line = item.get( key, '')
             if not line: 
                 continue
             if isinstance(line, unicode):
                 line = line.encode('utf-8', 'ignore')
             words = cutwords( line )
             newwords = self.get_valid_words ( words )
             for word in newwords:
                 if not word: continue
                 #位数少的数字
                 #日本字
                 
                 if word.isdigit() and len(word) <= 3: 
                     continue
                 hashwords[ word ] = 1
                 if word == '微电影' or word == '微影院' or word == '短片':
                     self._shortMovie = True
                 #年份
                 if len(word) == 7 and word.endswith('年') and word[:4].isdigit():
                     hashwords[ word[:4] ] = 1
                 #同义词
                 for w in wordTool.get_syn_words( word ):
                     hashwords[ w ] = 1
        return hashwords

    def extend_year_words(self, word ): 
        """ 年份"""
        if word.isdigit() and len(word) == 8:
            word = word[:4]
            if word >= "1970" and word <= cur_year:
                return [ word ]

        if word.isdigit() and len(word) == 6:
            word = word[:4]
            other = word[-2:]
            if word >= "1970" and word <= cur_year:
                return [ word ]

        if not word.endswith('年') and not word.endswith('年版'):
            return []
        if word.endswith('版'):
            word = word[:-6]
        else:
            word = word[:-3] 

        if word == 4 and word.isdigit():
            return [ word ]
        if word == 2 and word.isdigit():
            if int(word) <= 20:
                word = '20'+word
            else:
                word = '19'+word
            return [ word ]
        return []

    def extend_lang_words(self, word ): 
        if word.endswith('版'):
            word = word[:-3]
        if word == '': 
            return []
        if word in self.pth_lang:
            return ['国语', '汉语', '中文', '普通话', '华语']
        if word in self.mny_lang:
            return ['闽南语', '台语', '闽南话']
        if word in self.gdh_lang:
            return ['粤语', '广东话']

        num = len(word)
        if word.endswith('语') and num >= 6:
            w = word[:-3]+'文'
            return [w]

        if word.endswith('文') and num >= 6:
            w = word[:-3]+'文'
            return [w]

        return []

    def is_TV_match(self):
        is_TV_Flag = True
        vt = self.get_vide_type_name()
        if vt != '电视剧':
            is_TV_Flag = False
        return is_TV_Flag

    def build_index(self, wordinfos, fieldwords ):

        is_TV_Flag = self.is_TV_match()

        shortWords = {'短片':1, '微电影':1, '微影院':1}
        allwords = {}
        for item in wordinfos:
            words, infos = item
            for w in words:
                w = w.strip()
                if not w: 
                    continue
                if w in shortWords:
                    self._shortMovie = True
                allwords[w] = 1

            for w in infos:
                w = w.strip()
                if not w: continue
                allwords[w] = 1
                if w in shortWords:
                    self._shortMovie = True
                if not is_TV_Flag and infos[w] == 100:
                    if w.endswith('卫视') or w.endswith('卫视版'):
                        infos[w] = 50
        #扩展词
        otherwords = {}
        for w in allwords:
            if w in category_words:
                self._cateword = 1

            #英文＋数字
            if w.isalnum() and len(w) >= 3 and w[-1].isdigit() and w[:-2].isalpha():
                otherwords[w[:-1]] = 1
                otherwords[w[-1:]] = 1
                continue
            #年份
            exwords = self.extend_year_words( w )
            if exwords:
                for word in exwords:
                    otherwords[ word ] = 1
                continue
            #语言
            exwords = self.extend_lang_words( w )
            if exwords:
                for word in exwords:
                    otherwords[word] = 1
                continue
            #第×
            if w.startswith('第') and len(w) > 6:
                if w.endswith('部') or w.endswith('篇') or w.endswith('季') or w.endswith('节') or w.endswith('辑') or w.endswith('话'):
                    w = w[3:-3].strip()
                    if w.isdigit():
                        otherwords[w] = 1
                continue
            #罗马数字
            if w in self._romawords:
                w = self._romawords[w]
                otherwords[w] = 1
                continue
            #×版
            if len(w) > 6 and w.endswith('版'):
                w = w[:-3]
                otherwords[w] = 1
                continue
            #同义词@todo
            if w == '春晚':
                otherwords['春节'] = 1
                otherwords['联欢'] = 1
                otherwords['晚会'] = 1

            continue

        for w in otherwords:
            allwords[w] = 1

        #扩展其它域词表
        for w in fieldwords:
            w = w.strip()
            allwords[w] = 1
            if w in shortWords:
                self._shortMovie = True
            #年份
            exwords = self.extend_year_words( w )
            if exwords:
                for word in exwords:
                    allwords[word] = 1
            #其它
            if len(w)> 6 and w.endswith('版'):
                w = w[:-3]
                allwords[w] = 1
                continue

        return allwords

    def hashallwords(self, xinfo, xwords, _xwords):
        allwords = {}

        #标题文字分词结果
        for w in xwords:
            w = w.strip()
            allwords[w] = 1
            if w == '短片' or w == '微电影' or w == '微影院':
                self._shortMovie = True

            if w.isalnum() and len(w) >= 3 and w[-1].isdigit() and w[:-2].isalpha():
                allwords[w[:-1]] = 1
                allwords[w[-1:]] = 1
                continue
            #年份
            exwords = self.extend_year_words( w )
            if exwords:
                for word in exwords:
                    allwords[ word ] = 1
                continue
            #语言
            exwords = self.extend_lang_words( w )
            if exwords:
                for word in exwords:
                    allwords[word] = 1
                continue
            #第×
            if w.startswith('第') and len(w) > 6:
                if w.endswith('部') or w.endswith('篇') or w.endswith('季') or w.endswith('节') or w.endswith('辑') or w.endswith('话'):
                    w = w[3:-3].strip()
                    if w.isdigit():
                        allwords[w] = 1

        #扩展分词结果
        for w in _xwords:
            w = w.strip()
            allwords[w] = 1
            if w == '短片' or w == '微电影' or w == '微影院':
                self._shortMovie = True
            #年份
            exwords = self.extend_year_words( w )
            if exwords:
                for word in exwords:
                    allwords[word] = 1
            #其它
            if len(w)> 6 and w.endswith('版'):
                w = w[:-3]
                allwords[w] = 1
                continue

        is_TV_Flag = True
        vt = self.get_vide_type_name()
        if vt != '电视剧':
            is_TV_Flag = False

        for w in xinfo:
            w = w.strip()
            allwords[w] = 1
            if w == '短片' or w == '微电影' or w == '微影院':
                self._shortMovie = True
            if not is_TV_Flag and xinfo[w] == 100:
                if w.endswith('卫视') or w.endswith('卫视版'):
                    xinfo[w] = 50

            #语言
            words = self.extend_lang_words( w )
            if words:
                for word in words:
                    allwords[ word ] = 1
                continue
            #年份
            exwords = self.extend_year_words( w )
            if exwords:
                for word in exwords:
                    allwords[word] = 1

            #第×部／篇／季／辑
            allwords[w] = 1 
            if w.startswith('第') and len(w) > 6:
                if w.endswith('部') or w.endswith('篇') or w.endswith('季') or w.endswith('节') or w.endswith('辑') or w.endswith('话'):
                    w = w[3:-3].strip()
                    if w.isdigit():
                        allwords[w] = 1
                    continue

            #罗马数字
            if w in self._romawords:
                w = self._romawords[w]
                allwords[w] = 1
                continue

            #×版
            if len(w) > 6 and w.endswith('版'):
                w = w[:-3]
                allwords[w] = 1
                continue

        return allwords

    def get_word_df(self, word ):
        """词权重分配"""
        #听用词
        if word in self.stopwords:
            return self.stopwords[word]

        if word in self._cwordimps:
            return self._cwordimps[word] 

        if wordTool.is_low(word) :
            if config_match in [2, 3]:
                return 0.0
            return 0.05
        if word.isalpha():
            return 0.85
        if word.endswith('版') and word[:-3].isdigit():
            return 0.25
        if word.isdigit():
            return 0.85

        #if len(word) == 9 and len(word.decode('utf-8', 'ignore')) == 3 and word.endswith('篇') :
        #    return 1.5

        if is_japanese(word):
            return 0.5
        return 1.0

    def get_hit_info(self, words, infos, hashwords ):
        xhit, xsum, xlefts = 0, 0, []
        delflag = 0
        allwords = words + infos.keys()
        diffs = {}
        for word in allwords:
            wgt = self.get_word_df( word )
            xsum += wgt
            if word.isalnum() and len(word) >= 3 and word[-1].isdigit() and word[:-1].isalpha():
                if word[:-1] in hashwords and word[-1] in hashwords:
                    xhit += wgt
                    continue
            #正常命中
            if word in hashwords:
                xhit += wgt
                continue
            #去掉版／年
            if word.endswith('版') and len(word) >= 7 and word[:-3] in hashwords: 
                xhit += wgt
                continue
            if word.endswith('篇') and len(word) >= 7 and word[:-3] in hashwords: 
                xhit += wgt
                continue
            if word.endswith('年') and len(word) == 7:
                tmpword = word[:-3]
                if tmpword.isdigit() and tmpword in hashwords: 
                    xhit += wgt
                    continue
            #片／剧
            if word.endswith('片') and len(word) == 9 and word[:-3] in hashwords: 
                xhit += wgt
                continue
            if word.endswith('剧') and len(word) == 9 and word[:-3] in hashwords: 
                xhit += wgt
                continue

            #第×部／季／届／篇／节／册
            if word.startswith('第'):
                parts = self.utf8_order_pattern.findall( word )
                if len(parts)> 0 and parts[0] in hashwords:
                    xhit += wgt
                    continue
            #某人传
            if len(word) == 9 and word.endswith('传') and word[:-3] in hashwords:
                xhit += wgt
                continue

            #同义词
            ifMatch = False
            for w in wordTool.get_syn_words( word ):
                if w in hashwords:
                    xhit += wgt
                    ifMatch = True
                    break
            if ifMatch: 
                continue

            #语言
            if langTool.is_lang_word( word ):
                langs = langTool.get_lang_map( word )
                langs = langs.split(',')
                for w in langs:
                    if w == '': continue
                    if  w in hashwords:
                        xhit += wgt
                        ifMatch = True
                        break
            if ifMatch: 
                continue

            #×影／×剧
            if word.endswith('影') or word.endswith('剧') or word.endswith('版'):
                tmp = word[:-3]+"国"
                if len(tmp) >= 6 and tmp in hashwords:
                    xhit += wgt
                    continue
            #罗马数字
            if word in self._romawords:
                w = self._romawords[word]
                if w in hashwords:
                    xhit += wgt
                    continue

            if word in diffs: 
                ####...............
                continue
            diffs[ word ] = 1 
            if word not in infos:
                xlefts.append( word )
                continue
            #词类
            cat = int(infos[word])
            if cat == 200:  #语义极大变化词
                xsum += 1
                xlefts.append( word )
                continue
            elif cat == 100: #版本有较大区分，但是基本是一部剧
                xsum -= wgt
                xlefts.append( word )
                continue
            elif cat == 10:
                xsum -= 0.5
                xlefts.append( word )
                continue
            elif cat == 4:
                vt = self.get_vide_type_name()
                if not vt:
                    xlefts.append( word )
                    continue
                if word == '电影' or word == '电影版':
                    if vt != '电影':
                        xsum += 0.25
                elif word in self._catwords:
                    if vt != '动漫':
                        xsum += 0.25
                elif word == '综艺':
                    if vt != '综艺':
                        xsum += 0.25
                elif word == '电视剧':
                    if vt != '电视剧':
                        xsum += 0.25
                elif word == '记录片':
                    if vt != '记录片':
                        xsum += 0.25
                xlefts.append( word )
                continue
            elif cat == 5:
                w = word.replace('年', '').replace('版','')
                #数字年份／日期
                if w.isdigit():
                    xsum -= wgt
                    xlefts.append( word )
                else:
                    xlefts.append( word )
                continue
            elif cat == 50:
                xsum -= 0.1
                xlefts.append( word )
            elif cat > 0: #全集
                ######修复bugs:2017-2-15
                if cat == 2 and word in ['中国版', '中国', '内地', '内地版', '大陆', '大陆版', '国产', '国剧', '国产剧', '中国内地']:
                    xsum -= wgt
                elif cat == 6:
                    pass
                elif cat != 2:
                    xsum -= wgt
                #elif cat == 6:
                xlefts.append( word )
            #1-语言、2-区域、5-年份／日期、6-季／部、版本、10-人物
            #3-卫视、4-分类、8-全集、0-、100-、200-
        return xhit, xsum, xlefts

    def is_part_match(self):
        """ 是否版本一致 """
        return self._partok

    def is_join(self, xpart, ypart):
        xpart = xpart.encode('utf-8', 'ignore')
        ypart = ypart.encode('utf-8', 'ignore')

        xwords, _ = gpa.parse_words( xpart )
        ywords, _ = gpa.parse_words( ypart )

        xwords = [ w['word'] for w in xwords if w['word'] != "" and w['flag'] != "w" ]
        ywords = [ w['word'] for w in ywords if w['word'] != "" and w['flag'] != "w" ]

        xpart =  ''.join(xwords)
        ypart =  ' '.join(ywords)

        xpart = xpart.decode('utf-8', 'ignore')
        ypart = ypart.decode('utf-8', 'ignore')

        #xpart = re.sub(u'[美中日英俄法泰台粤国]{1,2}语版', u'', xpart)
        #ypart = re.sub(u'[美中日英俄法泰台粤国]{1,2}语版', u'', ypart)

        if len(xpart) <= 4 and xpart[-1:] == u'版':
            return True
        if len(ypart) <= 4 and ypart[-1:] == u'版':
            return True

        if xpart == u'动漫' or ypart == u'动漫':
            return True
        if xpart == u'电视剧' or ypart == u'电视剧':
            return True
        if xpart == u'电影' or ypart == u'电影':
            return True
        if xpart == u'动画' or ypart == u'动画':
            return True
        if xpart == u'综艺' or ypart == u'综艺':
            return True

        xwords = self.eword_pattern.findall(xpart)
        ywords = self.eword_pattern.findall(ypart)
        if len(xwords) > 0 and len(ywords) <= 0:
            xwords, ywords = [], []
        elif len(xwords) <= 0 and len(ywords)> 0:
            xwords, ywords = [], []

        for w in xpart:
            if is_unicode_hanzi(w):
                if w.isdigit(): continue
                if w == u'篇': continue
                if w == u'的': continue
                if w == u'版': continue
                if w == u'第': continue
                if w == u'部': continue
                if w == u'季': continue
                if w == u'集': continue
                xwords.append( w )
        for w in ypart:
            if is_unicode_hanzi(w):
                if w.isdigit(): continue
                if w == u'篇': continue
                if w == u'版': continue
                if w == u'的': continue
                if w == u'第': continue
                if w == u'部': continue
                if w == u'季': continue
                if w == u'集': continue
                ywords.append( w )
        if len(xwords) <= 0:
            return True
        if len(ywords) <= 0:
            return True
        com = set(xwords).intersection(set(ywords))
        tot = set(xwords).union(set(ywords))

        if len(tot) <= 0: return True
        if len(com) <= 0: return False
        if len(com) < len(tot) * 0.5:
            return True
        return True

    def get_diff_words(self):
        words = []
        for word in self._xlwords:
            words.append( word )
        for word in self._ylwords:
            words.append( word )
        #print '|'.join( words ), "======"
        return words

    def filte_by_name(self, yearflag ):
        """ 
        综合判断强拆分标记：
        1、原标记
        2、季或部+年份 
        3、yearflag: 
            < 0 未知
            > 0 年份不同
            = 0 年份相同
        """

        #原先有标记了
        if self._delflag > 0.9:
            return 1
        #"之"XXXX相同
        if self.bPartFlag != 0:
            return self._delflag

        x, y  = [],[]
        x_flag, y_flag = 0, 0
        for word in self._xlwords:
            if word.endswith('篇') and len(word) >= 6:
                x.append(word[:-3])
            if word.lower() in diff_unit_words:
                x_flag = 1
                
        for word in self._ylwords:
            if word.endswith('篇') and len(word) >= 6:
                y.append(word[:-3])
            if word.lower() in diff_unit_words:
                y_flag = 1

        if len(x) > 0 and len(y) > 0 and self._delflag < 0.01:
            self._delflag = 0.5
        if x_flag > 0 and y_flag > 0 and self._delflag < 0.01:
            self._delflag = 0.5


        # 无"季/部"差异
        if not self._diffparts:
            return self._delflag
        #要求是：知识库和专辑层次上的绑定
        if config_match != 1 and config_match != 2:
            return self._delflag

        #年份未知
        if yearflag < 0:
            return 0.5

        #年份相同:只默认第1季可省略
        if yearflag == 0:
            if self._diffparts[0] in ['第1部', '第1季', '1', 'i', '第1辑', '第1话']:
                return self._delflag

            if self._delflag > 0.1:
                return self._delflag

            #或者修改year_diff:
            xname = conv_string( self.album.get('name') ).strip().replace('国语版', '')
            yname = conv_string( self.video.get('name') ).strip().replace('国语版', '')
            if xname.startswith(yname):
                t = xname[len(yname):].strip()
                t = t.decode('utf-8', 'ignore')
                if re.search(u'第[0-9一二三四五六七八九十]+[季部辑话届篇]', t):
                    self._delflag = 0.5
            elif yname.startswith(xname):
                t = yname[len(xname):].strip()
                t = t.decode('utf-8', 'ignore')
                if re.search(u'第[0-9一二三四五六七八九十]+[季部辑话届篇]', t):
                    self._delflag = 0.5
            ########################
            self._delflag = 0.5
            return self._delflag


        #第一部、第一季、1、i、第1辑
        if self._diffparts[0] in ['第1部', '第1季', '1', 'i', '第1辑', '第1话']:
            return 0.5

        #年份差1年
        if yearflag == 1:
            return 0.5

        return 1

    def check_part_match_f(self, x, y):
        """
        检查季、部差异
        """
        xwords, xinfo = gpa.parse_words( x )
        ywords, yinfo = gpa.parse_words( y )

        xyears, yyears = [], [] 
        for w in xinfo:
           if xinfo[w] != 5:
               continue
           if w[:4].isdigit():
               xyears.append(w[:4])
        for w in yinfo:
           if yinfo[w] != 5:
               continue
           if w[:4].isdigit():
               yyears.append(w[:4])
        if len(xyears) == 1 and len(yyears) == 1 and yyears[0] == xyears[0]:
            t = int( yyears[0])
            if t >= 1985 and t <= 2021:
                self._title_year_flag = 1

        xparts, yparts = [], []
        for w in xinfo:
            if w == '微电影' or w == '微影院' or w == '短片':
                self._shortMovie = True
            if xinfo[w] != 6: continue
            if w in self._romawords:
                w = self._romawords[w]
                xparts.append(w)
                continue
            w = w.replace('第', '').replace('部','').replace('季','').replace('篇','').replace('辑','').replace('话','')
            xparts.append(w)
            
        for w in yinfo:
            if w == '微电影' or w == '微影院' or w == '短片':
                self._shortMovie = True
            if yinfo[w]  != 6: continue
            if w in self._romawords:
                w = self._romawords[w]
                yparts.append(w)
                continue
            w = w.replace('第', '').replace('部','').replace('季','').replace('篇','').replace('辑','').replace('话','')
            yparts.append(w)

        #无季或部词语
        if len(xparts) == 0 and len(yparts) == 0:
            return 0

        if len(xparts) == 1 and len(yparts) == 1:
            if xparts[0] == yparts[0]: return 1
            if xparts[0] != yparts[0] and config_match != 3: 
                return -1

        com = set(xparts).intersection(set(yparts))
        if len(com) > 0:
            return 1

        tot = set(xparts).union(set(yparts))
        diff = list(tot.difference( com ) )
        if len(xparts) > 0 and len(yparts) > 0 and config_match != 3 :
            return -1
        self._diffparts = diff 

        return 0

    def parse_main_name(self):
        xname = conv_string( self.album.get('name') ).strip()
        yname = conv_string( self.video.get('name') ).strip()

        if len(yname) > 6  and yname.startswith('《') and yname.endswith('》'):
            yname = yname[3:-3].strip()
        if len(xname) > 6  and xname.startswith('《') and xname.endswith('》'):
            xname = xname[3:-3].strip()
			
        rt = self.is_name_equal( xname, yname  )
        if not rt:
            return rt
        pos = xname.find('之')
        if pos >= 9 and len(xname)-pos>=12:
            self._partok = 1
            self.bPartFlag = 1
            return rt
        
        pos = yname.find('之')
        if pos >= 9 and len(yname)-pos>=12:
            self._partok = 1
            self.bPartFlag = 1
            return rt
        pos = xname.find(':')

        flag = self.check_part_match_f(xname, yname)
        if flag > 0.99:
            self._partok = 1
            self.bPartFlag = 1
        elif flag < 0 and config_match != 3 :
            self._delflag = 1

        return rt
        
    def is_name_equal(self, xname, yname ):
        #等同
        if xname == yname:
            return 1

        #去标点
        w1 = self.cleanString( xname ) 
        w2 = self.cleanString( yname ) 
        if w1 == w2:
            return 1
        #转拼音
        w1 = self.toPinYin(w1)
        w2 = self.toPinYin(w2)
        if w1 == w2:
            return 1
        #去英文标题
        xname_fr = conv_string( self.album.get('name_fr', '') )
        if len(xname_fr) > 3 and xname.endswith(xname_fr):
            w1 = xname[:-len(xname_fr)].strip()
            if w1 == yname:
                return 1
        yname_fr = conv_string( self.video.get('name_fr', '') )
        if len(yname_fr) > 3 and yname.endswith(yname_fr):
            w2 = yname[:-len(yname_fr)].strip()
            if w2 == xname:
                return 1

        ##副标题
        if self._dealSubtitle(xname, yname):
            return 1

        #解决   XXX1:YYY
        if self._dealHeadTail(xname, yname ):
            return 1

        return 0

    def _dealHeadTail(self, xname, yname ): 
        uxname = xname.decode('utf-8', 'ignore')
        uyname = yname.decode('utf-8', 'ignore')
        uxname = uxname.replace(u' 国语版', u'')
        uxname = uxname.replace(u' tv版', u'')
        uxname = uxname.replace(u' 国语', u'')

        uyname = uyname.replace(u' 国语版', u'')
        uyname = uyname.replace(u' tv版', u'')
        uyname = uyname.replace(u' 国语', u'')

        if len(uxname) < len(uyname):
            tmp = uxname
            uxname = uyname
            uyname = tmp
        pos = uxname.find(u':')
        if pos < 0:
            pos = uxname.find(u'：')
        if pos < 0:
            pos = uxname.find(u'之')
        if pos < 3:
            return 0 
        if len(uxname) - pos < 4:
            return 0
        head = uxname[:pos]
        tail = uxname[pos+1:]
        if not uyname.endswith( tail):
            return 0
        uyname = uyname[:-len(tail)].strip()

        if not uyname:
            return 0

        diff = None
        if uyname[-1] in [u':', u'之',u'-', u'－', u'—', u'：']:
            uyname = uyname[:-1].strip()

        if uyname.startswith( head ):
            diff = uyname[len(head):].strip()
        elif head.startswith( uyname ):
            diff = head[len(uyname):].strip()
        if diff is None:
            return 0
        if diff.isdigit():
            return 1
        return 0

    def get_other_names(self, video):

        name_ot = conv_string( video.get('name_ot', '') )
        if not name_ot: 
            return []

        name_ot = name_ot.replace('爱拍电影', '')
        name_ot = name_ot.replace('爱拍电影短片', '')
        othernames = name_ot.replace(',',';').replace('|',';').replace('/',';')
        
        othernames = othernames.split(';')
        result = []
        for name in othernames:
            name = name.strip()
            if name == '': continue
            if name == '阿u': continue
            if name == '蓝迪': continue
            if name == '蓝迪系列动画': continue

            result.append( name )
        if len(result) > 1:
            return result

        if len(result) == 0:
            return result
        line = result[0]
        if len(line) >= 80:
            result = []
            words = line.split()
            for word in words:
                if word.isalnum():
                    continue
                if len( word ) >= 12:
                    result.append( word )
            pass
        return result

    def is_english_line22(self, line ): 
        enum, cnum, dnum = self.parse_line_info( line )
        if cnum >= enum:
            return False
        if enum < 3:
            return False
        return True

    def parse_line_info(self, line ): 
        enum, cnum, dnum = 0, 0, 0 
        if isinstance(line, str):
            line = line.decode('utf-8', 'ignore')
        if not isinstance( line, unicode):
            return enum, cnum, dnum
        for ch in line:
            if is_unicode_hanzi(ch):
                cnum += 1
                continue
            if ch.isdigit():
                dnum += 1
                continue
            if is_english( ch ): 
                enum += 1
        return enum, cnum, dnum

    def get_all_name( self, video ):
        alias_flag = True
        vt = video.get('video_type', None )
        if vt in [ 4, '4' ]:
            t1 = video.get('screen_year', None )
            t2 = video.get('screen_time', None )
            if not t1 and not t2:
                alias_flag = False
            
        enames, elen = [], 0

        ename = conv_string( video.get('name_fr') ).strip()
        if self.is_english_line22( ename ):
            elen = len(ename)
            enames.append( ename )
        else:
            ename = ''

        #主要名字
        cnames, dups = [], {}
        sup = []
        cname = conv_string( video.get('name') ).strip()
        if elen > 0 and len(cname) > elen and cname.endswith( ename ):
            cname = cname[:-elen ].strip()
        if cname != "":
            cname = gpa._convdigit( cname )
            #重大修复:2017-02-23
            cname = re.sub('第[0-9]+集', '', cname)
            if cname.startswith('《') and cname.endswith('》') and len(cname) > 6 :
                cname = cname[3:-3].strip()
			
            cnames.append(cname)
            sup.append( 1 )
            dups[ cname ] = 1

        firstname = cname
        #其它名字
        others = self.get_other_names( video )   
        for cname in others:
            cname = cname.strip()
            #前缀作为别名
            if firstname.startswith(cname):
                temp = firstname[len(cname):]
                if temp.startswith('之') or temp.startswith(':') or temp.startswith('：'):
                    continue
                if temp in diff_part_words: continue

            if elen and cname.endswith( ename ):
                cname = cname[:-elen].strip()
            if not cname: continue
            cname = gpa._convdigit( cname )
            if cname in dups: continue
            dups[ cname ] = 1
            cnames.append( cname )
            sup.append( 1 )
        allnum = len(cnames)
        others = []
        #####################
        # 综艺要求有年份
        
        #####################
        if alias_flag and cname not in alias_names:
           if cname.endswith('国语版'):
               cname = cname[:-9]
           elif cname.endswith('粤语版'):
               cname = cname[:-9]
           elif cname.endswith(' 粤语'):
               cname = cname[:-7]
           elif cname.endswith(' 国语'):
               cname = cname[:-7]
           elif cname.endswith('英文版'):
               cname = cname[:-9]
           elif cname.endswith('普通话版'):
               cname = cname[:-12]

        if alias_flag and cname in alias_names:
            if ename =='' and is_english_line( cname ):
                ename = cname
            #置信度低一些 0.8
            others.extend( alias_names[ cname ])

        if cname.find('春节联欢晚会') != -1:
            tname = cname.replace('春节联欢晚会', '春晚')
            others.append( tname )
        elif cname.find('春晚') != -1:
            tname = cname.replace('春晚', '春节联欢晚会')
            others.append( tname )

        for cname in others:
            cname = cname.strip()
            if elen and cname.endswith( ename ):
                cname = cname[:-elen].strip()
            if not cname: continue
            if firstname.startswith(cname):
                temp = firstname[len(cname):].strip()
                if temp.startswith('之') or temp.startswith(':') or temp.startswith('：'):
                    continue
                if temp in diff_part_words: continue

            cname = gpa._convdigit( cname )
            if cname in dups: continue
            dups[ cname ] = 1
            cnames.append( cname )
            sup.append( 0.96 )
        return cnames, enames, allnum, sup

    def parse_all_cname(self, cnames):
        if not cnames:
            return []
        result = []
        for cname in cnames:
            months = re.findall('[- _]([0-9]+月)$', cname )
            if months and cname.endswith( months[-1]):
                cname = cname[:-len(months[-1])]

            words, infos = gpa.parse_words( cname )
            words = self.get_valid_words ( words )
            result.append( ( words, infos ) ) 
        return result

    def get_title_delflag(self, hash1, hash2 ):
        words = ['两部曲', '三部曲', '四部曲', '五部曲']
        for word in words:
            if word in hash1 and word not in hash2:
                return 1
            if word not in hash1 and word in hash2:
                return 1
        return 0

    def parse_all(self):
        delflag = 0
        cnames_1, enames_1, orinum_1, sups_1 = self.get_all_name( self.album )           
        cnames_2, enames_2, orinum_2, sups_2 = self.get_all_name( self.video )           

        if len(enames_1) > 0 and cnames_2[0] == enames_1[0]:
           cnames_1.append( enames_1[0] )
           sups_1.append( 0.96 )

        if len(enames_2) > 0 and enames_2[0] == cnames_1[0]:
           cnames_2.append( enames_2[0] )
           sups_2.append( 0.96 )

        wordinfo_1 = self.parse_all_cname( cnames_1 )
        wordinfo_2 = self.parse_all_cname( cnames_2 )

        num1, num2 = len(cnames_1), len(cnames_2)
        _xwords = self._getFieldWords( self.album )
        _ywords = self._getFieldWords( self.video )

        hash1 = self.build_index(wordinfo_1, _xwords)
        hash2 = self.build_index(wordinfo_2, _ywords)
        #X部曲 
        delflag = self.get_title_delflag( hash1, hash2 )

        #careflags = {5:1, 6:1, 100:1, 200:1}
        careflags = {5:1, 6:1}
        matches1, matches2 = [], []
        xlefts, ylefts = {}, {}

        careflags_acc_1 = []
        careflags_acc_2 = []
        for i in range(num1):
            words1, infos1 = wordinfo_1[i]
            if i == 0:
                for w in infos1:
                    if infos1[w] in careflags:
                        careflags_acc_1.append( infos1[w] )

            item = self.get_hit_info( words1, infos1, hash2 )
            matches1.append( item)
            #未命中词 
            for w in item[-1]:
                if w == '微电影': continue
                if w == '微影院': continue
                if w == '微电影版': continue
                if i >= orinum_1: continue
                if w in infos1:
                    if i > 0 and infos1[w] not in [ 5, 6]: continue
                    #排除主要名字版本
                    if i > 0 and infos1[w] in careflags_acc_1: continue
                    xlefts[w] = infos1[w]
                    continue
                xlefts[w] = 0
            
        for j in range(num2):
            words2, infos2 = wordinfo_2[j]
            if j == 0:
                for w in infos2:
                    if infos2[w] in careflags:
                        careflags_acc_2.append( infos2[w] )
            item = self.get_hit_info( words2, infos2, hash1 )
            matches2.append( item )
            #未命中词 
            for w in item[-1]:
                if w == '微电影': continue
                if w == '微影院': continue
                if w == '微电影版': continue
                if j >= orinum_2: 
                    continue
                if w in infos2:
                    if j > 0 and infos2[w] not in [5, 6]: continue
                    #排除主要名字版本
                    if j > 0 and infos2[w] in careflags_acc_2: continue
                    ylefts[w] = infos2[w]
                    continue
                ylefts[w] = 0

        #self._diffparts = xlefts = ylefts
        _ratio = 0.98 
        _pair, _max = None, -1000
        #_pair = 0, 0
        #之 括号 冒号 破折号 斜线
        for i in range(num1):
            for j in range(num2):
                com = set( wordinfo_1[i][0] ).intersection( set(wordinfo_2[j][0]) )
                if len(com) <= 0 and i != 0 and j != 0: 
                    continue
                xhit, xsum, _ = matches1[i]
                yhit, ysum, _ = matches2[j]
                sc = 0
                if xsum + ysum > 0.001:
                    sc = (xhit + yhit) / (xsum+ysum+0.0)
                
                if sc *1.2 < _max: continue

                #if w in infos2 and infos2[w] in careflags: 
                #    ylefts[w] = infos2[w]
                #    continue
                if i != 0 and j != 0: sc *= _ratio
                addratio = 1
                
                if cnames_1[i].startswith( cnames_2[j] ):
                    ylen = len(cnames_2[j])
                    tail = cnames_1[i][ylen:]
                    tail = tail.strip()
                    tlen = len(tail)
                    if tail.startswith('之') or tail.startswith('：') or tail.startswith(':'):
                        addratio = 1.2
                    elif tail.startswith('/') or tail.startswith('—') or tail.startswith('-'):
                        addratio = 1.2
                    elif tail.startswith('(') and tlen >= ylen + 11:
                        if tail.endswith(')') or tail.endswith('）'):
                            addratio = 1.2
                    elif tail.startswith('（') and tlen >= ylen + 13:
                        if tail.endswith(')') or tail.endswith('）'):
                            addratio = 1.2

                elif cnames_2[j].startswith( cnames_1[i] ):
                    ylen = len(cnames_1[i])
                    tail = cnames_2[j][ylen:]
                    tail = tail.strip()
                    tlen = len(tail)
                    if tail.startswith('之') or tail.startswith('：') or tail.startswith(':'):
                        addratio = 1.2
                    elif tail.startswith('/') or tail.startswith('—') or tail.startswith('-'):
                        addratio = 1.2
                    elif tail.startswith('(') and tlen >= ylen + 11:
                        if tail.endswith(')') or tail.endswith('）'):
                            addratio = 1.2
                    elif tail.startswith('（') and tlen >= ylen + 13:
                        if tail.endswith(')') or tail.endswith('）'):
                            addratio = 1.2

                #if i != 0 or j != 0:
                #    sc *= 0.98
                sc *= addratio
                sc = min(sc, 1.0)
                if i != 0 and j != 0: sc *= 0.99

                if i != 0 and j != 0 and wordTool.is_bad_alias( cnames_1[i]) and wordTool.is_bad_alias(cnames_2[j]):
                    sc *= 0.88
                elif i != 0 and sc >= 0.90 and wordTool.is_bad_alias(cnames_1[i]):
                    sc *= 0.88
                elif j != 0 and sc >= 0.90 and wordTool.is_bad_alias(cnames_2[j]):
                    sc *= 0.88
                    pass
                sc *= min( sups_1[i], sups_2[j])
                if sc < _max: continue
                _max = sc
                    
                _pair = i, j   

        #最终得分
        sim = _max
        if _pair is None:
            pass
        i,j = _pair
        #print cnames_1[i], '====', i
        #print cnames_2[j], '====', j
        self._xlwords = matches1[ _pair[0] ][2]
        self._ylwords = matches2[ _pair[1] ][2]
        #主名:
        part_match = self.check_first_name( cnames_1[0], cnames_2[0], wordinfo_1[0][1], wordinfo_2[0][1] )
        """
        0-可忽略 1-语言 2-地域 3- 4-类别词 5-年份/日期(数字或农历) 
        6-part 7- 8-全集 10-人物 100-重要词 200-加高权重
        """
        #语言
        xdiffs = [ w for w in xlefts if xlefts[w] == 1 ]
        ydiffs = [ w for w in ylefts if ylefts[w] == 1 ]
        lang_err = self.check_lang_err( xdiffs, ydiffs )
        if lang_err > 0.99 and config_match == 1: 
            delflag = 1

        #区域
        xdiffs = [ w for w in xlefts if xlefts[w] == 2 ]
        ydiffs = [ w for w in ylefts if ylefts[w] == 2 ]
        area_err = self.check_area_err( xdiffs, ydiffs )

        #print 'area_err = ', area_err
        # 专辑层和知识库层
        if area_err > 0.99 and config_match != 3: 
            delflag = 1
            sim *= 0.9
        elif area_err > 0.499 and config_match != 3:
            delflag = area_err

        #电视台
        xdiffs = [ w for w in xlefts if xlefts[w] == 3 ]
        ydiffs = [ w for w in ylefts if ylefts[w] == 3 ]
        tv_err = self.check_tv_err( xdiffs, ydiffs )
        #仅专辑层绑定
        if tv_err and config_match == 1: 
            delflag = 1

        #季/部
        xparts = [ w for w in xlefts if xlefts[w] == 6 ]
        yparts = [ w for w in ylefts if ylefts[w] == 6 ]
        part_err = self.check_part_err( xparts, yparts )
        if part_err < 0 and part_match == 0: 
            part_match = 1
        elif part_err > 0 :
            part_match = -1
        elif len(xparts) > 1 and len(yparts) == 0: #有多个版本词
            if config_match != 3 and delflag <= 0.01:
               delflag = 0.5
               xparts = []
        elif len(yparts) > 1 and len(xparts) == 0:
            if config_match != 3 and delflag <= 0.01:
               delflag = 0.5
               yparts = []

        elif len(yparts) == 1 and len(xparts) == 0:
            pass

        self._diffparts = xparts + yparts
        if part_err and config_match != 3: 
            delflag = 1

        #全集
        full_err = 0
        if '全集' in xlefts and yparts:
            full_err = 1
        elif '全集' in ylefts and xparts:
            full_err = 1

        if full_err and config_match != 3: 
            delflag = 1

        #年份
        xdiffs = [ w for w in xlefts if xlefts[w] in [5,6] ]
        ydiffs = [ w for w in ylefts if ylefts[w] in [5,6] ]
        year_err = self.check_year_err( xdiffs, ydiffs )
        if year_err == 100:
            year_err = 1
            sim *= 0.95
        
        #标题年份检查
        tmpxyears, tmpyyears = [],[]
        for w in wordinfo_1[0][1]:
            if wordinfo_1[0][1][w] != 5: continue
            if w[:4].isdigit():
                tmpxyears.append( w[:4] )
        for w in wordinfo_2[0][1]:
            if wordinfo_2[0][1][w] != 5: continue
            if w[:4].isdigit():
                tmpyyears.append( w[:4] )
        tmpxyears = list(set(tmpxyears))
        tmpyyears = list(set(tmpyyears))
        if len(tmpxyears) == 1 and len(tmpyyears) == 1 and tmpxyears[0] == tmpyyears[0]:
            t = int(tmpxyears[0])
            if t >= 1985 and t <= 2021:
                self._title_year_flag = 1

        if year_err and config_match != 3: 
            delflag = 1

        #版本词:100
        ver_err = 0
        xdiffs = [ w for w in xlefts if xlefts[w] == 100 ]
        ydiffs = [ w for w in ylefts if ylefts[w] == 100 ]
        xname, yname = cnames_1[0], cnames_2[0]

        if len(xdiffs ) == 1 and xdiffs[0] in ['续', '续集'] and  xname.find('续') == -1:
            xdiffs = []
        if len(ydiffs ) == 1 and ydiffs[0] in ['续', '续集'] and  yname.find('续') == -1:
            ydiffs = []
        if xdiffs or ydiffs:
            ver_err = 1
        if ver_err and config_match != 3: 
            delflag = 1

        #其它词:200
        word_err = 0
        xdiffs = [ w for w in xlefts if xlefts[w] == 200 ]
        ydiffs = [ w for w in ylefts if ylefts[w] == 200 ]
        if xdiffs or ydiffs :
            word_err = 1

        if word_err:
            delflag = 2

        #上中下
        if delflag == 0 and config_match != 3:
            xname, yname = cnames_1[0], cnames_2[0]
            xname = xname.replace('》', ' ').replace(':', ' ')
            yname = yname.replace('》', ' ').replace(':',' ')
            if xname.endswith(' 上'):
                if yname.endswith(' 下') or yname.endswith('中'):
                    delflag = 1
            if xname.endswith(' 下'):
                if  yname.endswith(' 上') or yname.endswith(' 中'):
                    delflag = 1
        #之系列
        #重大改变:2017-02-23
        a, b = _pair
        if sim < 0.98 :
            part_match = 0
        elif  a != 0 and b != 0:
            part_match = 0

        if delflag == 0 and config_match != 3 and part_match != 1 :
            xname, yname = cnames_1[0], cnames_2[0]
            xname = xname.decode('utf-8', 'ignore')
            yname = yname.decode('utf-8', 'ignore')
            pos1 = xname.find(u'之')
            pos2 = yname.find(u'之')
            pos = max(pos1, pos2)
            pos1 = min(pos1, pos2)
            if pos1 >= 4:
                pos = pos1
            if pos >= 3:
                if xname[:pos] == yname[:pos] and len(xname) >= pos + 4 and len(yname)>= pos + 4:
                    xpart, ypart = xname[pos:].strip(), yname[pos:].strip()
                    if xpart[0] in [u'之', u':',u'：']:
                        xpart = xpart[1:].strip()
                    if ypart[0] in [u'之', u':',u'：']:
                        ypart = ypart[1:].strip()
                    head1 = xname[:pos]
                    head2 = yname[:pos]
                    
                    if len(xpart)>=2 and len(ypart)>=2 and not self.is_join( xpart, ypart) :
                        if xlefts or ylefts: 
                            delflag = 1
                    elif head1 == head2 and xpart == ypart and len(xpart) >= 3 and len(head1)>= 3:
                            part_match = 1
                            self._diffparts = []
                    elif head1 == head2 and xpart.endswith(ypart) and len(ypart) >= 3:
                        diff = xpart[:-len(ypart)].strip()
                        if diff and diff[-1] in [u'之', u':',u'：', u'-', u'—']:
                            diff = diff[:-1].strip()
                        dlen = len(diff)
                        if dlen == 0:
                            part_match = 1
                        elif dlen <= 2 and diff.encode('utf-8', 'ignore') in self._romawords:
                            part_match = 1
                            self._diffparts = []
                        elif dlen <= 2 and diff.isdigit():
                            part_match = 1
                            self._diffparts = []
                    elif head1 == head2 and len(xpart) >= 3 and ypart.endswith(xpart) :
                        diff = ypart[:-len(xpart)].strip()
                        if diff and diff[-1] in [u'之', u':',u'：', u'-', u'—']:
                            diff = diff[:-1].strip()
                        dlen = len(diff)
                        if dlen == 0:
                            part_match = 1
                        elif dlen <= 2 and diff.encode('utf-8', 'ignore') in self._romawords:
                            part_match = 1
                            self._diffparts = []
                        elif dlen <= 2 and diff.isdigit():
                            part_match = 1
                            self._diffparts = []

        #第一名字年份
        if delflag == 0 and config_match != 3 :
            xname, yname = cnames_1[0], cnames_2[0]
            xname = xname.decode('utf-8', 'ignore')
            yname = yname.decode('utf-8', 'ignore')
            xyears = re.findall(u'[0-9]{4,}', xname) 
            yyears = re.findall(u'[0-9]{4,}', yname) 
            if len(xyears) == 1 and len(yyears) == 1:
                xyear, yyear = int(xyears[0]), int(yyears[0])
                if xyear > 1900 and xyear <= 2020 and yyear > 1900 and yyear < 2020 and xyear != yyear:
                    delflag = 1
        #上中下
        if delflag == 0 and config_match != 3 :
            for w in xlefts:
                if w in care_part_words:
                    delflag = 0.5
            for w in ylefts:
                if w in care_part_words:
                    delflag = 0.5

        self.bPartFlag = part_match
        self._delflag = delflag
        if part_match>0: 
            self._partok = 1 

        #主要名字
        xname = ' '.join( wordinfo_1[0][0] )
        yname = ' '.join( wordinfo_2[0][0] )
        if xname.startswith(yname) and sim < 0.8:
            tail = xname[len(yname):].strip()
            enum, cnum, dnum = self.parse_line_info( tail )
            if enum > 3 and cnum == 0 and len( tail.split() ) > 1:
                sim *= 1.2
                if sim < 0.8: sim = 0.8
        elif yname.startswith(xname) and sim < 0.8:
            tail = yname[len(xname):].strip()
            enum, cnum, dnum = self.parse_line_info( tail )
            if enum > 3 and cnum == 0 and len(tail.split() ) > 1:
                sim *= 1.2
                if sim < 0.8: sim = 0.8
        elif sim < 0.80:
            xname = ''.join( wordinfo_1[0][0] )
            yname = ''.join( wordinfo_2[0][0] )
            head = ''
            if cnames_1[0].endswith( yname ):
                head = cnames_1[0][:-len( yname)].strip()
            elif cnames_2[0].endswith( xname ):
                head = cnames_2[0][:-len( xname)].strip()
            if len(head) > 9:
                if head.endswith('—') or head.endswith('：') or head.endswith(':'):
                    sim *= 1.2
                elif head.endswith('之') or head.endswith('-'):
                    sim *= 1.2
        
        #words = self.get_diff_words()
        #print ' | '.join(words)
        return sim 


    def check_year_err(self, xdiffs, ydiffs ):
        if not xdiffs or not ydiffs:
            return 0 
        xdays, ydays = [], []
        xyears, yyears = [], []
        xhan, yhan = [], []

        for word in xdiffs:
            word = word.replace('年', '').replace('版','').strip()
            wlen = len(word)
            if word.isdigit() and wlen>=4:
                xyears.append(word[:4])
                if wlen == 8:
                    xdays.append( word )
            elif len(word) == 3:
                xhan.append(word)
        for word in ydiffs:
            word = word.replace('年', '').replace('版','').strip()
            wlen = len(word)
            if word.isdigit() and wlen>=4:
                yyears.append(word[:4])
                if wlen == 8:
                    ydays.append( word )
            elif len(word) == 3:
                yhan.append(word)

        if xyears and yyears:
            com = set(xyears).intersection(set(yyears))
            if len(com) <= 0:
                return 1
        #日期差异
        if xdays and ydays and len(xdays) == 1 and len(ydays) == 1:
            if xdays[0] != ydays[0] and config_match == 1:
                return 1

        if xhan and yhan:
            com = set(xhan).intersection(set(yhan))
            if len(com) <= 0:
                return 100

        return 0

    def check_part_err(self, xdiffs, ydiffs ):
        xparts, yparts = [], []
        for w in xdiffs:
            #罗马数字
            if w in self._romawords:
                w = self._romawords[w]
                xparts.append( w )
                continue
            if len(w) >= 9 and w.startswith('第') and w[-3:] in ['部', '季', '辑', '话']:
                w = w[3:-3]
                xparts.append( w )
                continue
            xparts.append( w )
        for w in ydiffs:
            #罗马数字
            if w in self._romawords:
                w = self._romawords[w]
                yparts.append( w )
                continue
            if len(w) >= 9 and w.startswith('第') and w[-3:] in ['部', '季', '辑', '话']:
                w = w[3:-3]
                yparts.append( w )
                continue
            yparts.append( w )
        if len(xparts) > 0 and len(yparts) > 0:
            com = set(xparts).intersection(set(yparts))
            if len(com) <= 0:
                return 1
            else:
                return -1
        else:
            self._diffparts = xparts + yparts
        return 0

    def check_lang_err(self, xdiffs, ydiffs ):
        if not xdiffs and not ydiffs:
            return 0
        if not xdiffs or not ydiffs:
            return 0.5

        x = ';'.join(xdiffs)
        y = ';'.join(ydiffs)

        if '有字' in x and '无字' in y:
            return 1
        if '有字' in y and '无字' in x:
            return 1
        if '有声' in x and '无声' in y:
            return 1
        if '有声' in y and '无声' in x:
            return 1

        x, y = [], []
        for la in xdiffs:
            words = langTool.format_langstr(la)
            for word in words:
                if not word.endswith('话') and not word.endswith('语') and not word.endswith('方言'):
                    continue
                x.append( word )
        for la in ydiffs:
            words = langTool.format_langstr(la)
            for word in words:
                if not word.endswith('话') and not word.endswith('语') and not word.endswith('方言'):
                    continue
                y.append( word )
        if not x and not y:
            return 0
        if not x or not y:
            return 0.5
        com = set(x).intersection(set(y))
        if len(com) <= 0:
            return 1
        return 0

    def check_tv_err(self, xdiffs, ydiffs ):
        if not xdiffs or not ydiffs:
            return 0

        x, y = [], []
        for w in xdiffs:
            word = w[:-3] if w.endswith('版') else w
            x.append(w)

        for w in ydiffs:
            word = w[:-3] if w.endswith('版') else w
            y.append(w)
        com = set(x).intersection(set(y))
        if len(com) <= 0:
            return 1

        return 0

    def check_area_err(self, xdiffs, ydiffs ):
        if not xdiffs and not ydiffs:
            return 0

        tmpwords = { '国产':1, '中国':1, '内地':1, '大陆':1, '中国版':1, '大陆版':1, '内地版':1, '国产剧':1, '大陆剧':1, '内地剧':1 }
        if not xdiffs or not ydiffs:
            if len(xdiffs) > 0:
                if xdiffs[0] in tmpwords:
                    return 0
            elif len(ydiffs) > 0:
                if ydiffs[0] in tmpwords:
                    return 0
            return 0.5
        x, y = [], []
        for la in xdiffs:
            words = areaTool.format_areastr(la)#.split(',')
            for word in words:
                word = word.strip()
                if not word:
                    continue
                x.append( word )

        for la in ydiffs:
            words = areaTool.format_areastr(la)#.split(',')
            for word in words:
                word = word.strip()
                if not word:
                    continue
                y.append( word )

        if not x and not y:
            return 0
        if not x or not y:
            return 0.5
        com = set(x).intersection(set(y))
        if len(com) <= 0:
            return 1
        return 0


    def extract_parts(self, name ): 
        if isinstance(name, str):
            name = name.decode('utf-8', 'ignore')
        if not isinstance(name, unicode):
            return []
        xparts = self.bri_updwn_pattrn.findall( name )
        xparts += self.updn_pattern2.findall( name )
        return xparts

    def extract_parts2(self, xinfo ):
        xtvpar, xparts = [], []
        for w in xinfo:
            if w.endswith('版'):
                w = w[:-3]
            #卫视版 
            if w.endswith('卫视') and len(w) >= 12 :
                w = w[:-6]
                xtvpar.append( w[:-6])
                continue
            if w not in xinfo: continue

            #季／部
            if xinfo[w] != 6: continue 

            #罗马数字
            if w in self._romawords:
                w = self._romawords[w]
                xparts.append( w )
                continue
            if len(w) >= 7 and w.startswith('第') and w[-3:] in ['部', '季', '辑', '话']:
                w = w[3:-3].strip()
            if w == '': continue
            xparts.append( w )
        return xtvpar, xparts

    def check_first_name(self, xname, yname, xinfo, yinfo ):
        part_match = 0

        #第*部
        uxname = xname.decode('utf-8', 'ignore')
        uyname = yname.decode('utf-8', 'ignore')
        xnums = re.findall(u'第([0-9]+)[^集0-9]', uxname) 
        ynums = re.findall(u'第([0-9]+)[^集0-9]', uyname) 
        #xnums = self.utf8_order_pattern.findall( xname )
        #ynums = self.utf8_order_pattern.findall( yname )
        if xnums and ynums:
            com = set(xnums).intersection(set(ynums))
            if len(com) <= 0:
                part_match = -1
            else:
                part_match = 1

        #上中下
        xparts = self.extract_parts( xname )
        yparts = self.extract_parts( yname )
        if len(xparts) > 0 and len(yparts) > 0:
            com = set(xparts).intersection(set(yparts))
            if len(com) <= 0:
                part_match = -1
            elif part_match == 0:
                part_match = 1
        elif len(xparts) > 0 or len(yparts) > 0:
            part_match = -1

        xtvpar, xparts = self.extract_parts2( xinfo )
        ytvpar, yparts = self.extract_parts2( yinfo )

        #电视版本
        if ytvpar and xtvpar:
            com = set(xtvpar).intersection( set(ytvpar) )
            if len(com) <= 0: 
                part_match = -1
        #季/部
        xparts = list(set(xparts))
        yparts = list(set(yparts))
        if xparts and yparts:
            com = set(xparts).intersection( set(yparts) )
            if len(com) <= 0:
                part_match = -1
            elif part_match == 0 and len(com) == len(xparts) and len(com) == len(yparts):
                part_match = 1

        #返回结果
        return part_match

    def parse(self):

        if not isinstance(self.album, dict):
            return 0.0
        if not isinstance(self.video, dict):
            return 0.0

        rt = self.parse_main_name()
        if rt >= 0.999:
            return rt

        rt = self.parse_all()
        return rt


    def _dealSubtitle(self, xname, yname ):
        """副标题"""
        if xname.startswith(yname):
            other = conv_string( self.video.get('sub_tile', '') )
            num = len(other)
            part = xname[len(yname):].strip()
            if num >= 6 and part.endswith(other):
                part = part[:-len(other)].strip()
            part = part.strip() 
            if part == "" or part == "之" or part == ":" or part == "：":
                return True

        if yname.startswith(xname):
            other = conv_string( self.album.get('sub_tile', '') )
            num = len(other)
            part = yname[len(xname):].strip()
            if num >= 6 and part.endswith(other):
                part = part[:-len(other)].strip()
            part = part.strip() 
            if part == "" or part == "之" or part == ":" or part == "：":
                return True
        return False

    def _dealOtherTitle(self):
        #名称
        le_names, do_names = [], []
        xnames, ynames = [], []

        xdup, ydup = {}, {}
        xname = conv_string( self.album.get('name') )
        namestr = self._dealTitle( xname )
        if namestr:
            le_names.append( namestr )
            xdup[ namestr ] = 1
            xnames = [ xname ]

        name_ot = conv_string( self.album.get('name_ot', '') )
        name_ot = name_ot.replace('爱拍电影', '')
        name_ot = name_ot.replace('爱拍电影短片', '')
        othernames = name_ot.replace(',',';').replace('|',';').replace('/',';').split(';')
        for name in othernames:
            name = name.strip()
            namestr = self._dealTitle( name )
            if namestr != "" and namestr not in xdup:
                if namestr == '微电影': continue
                if namestr == '电影': continue
                if namestr == '电视剧': continue
                if namestr == '短片': continue
                xdup[ namestr ] = 1
                xnames.append( name )
                le_names.append( namestr )
        
        
        yname = conv_string( self.video.get('name') )
        namestr = self._dealTitle( yname )
        if namestr:
            ynames = [ yname ]
            do_names.append( namestr )
            ydup[ namestr ] = 1

        name_ot = conv_string( self.video.get('name_ot', '') )
        name_ot = name_ot.replace('爱拍电影', '')
        name_ot = name_ot.replace('爱拍电影短片', '')
        othernames = name_ot.replace(',',';').replace('|',';').replace('/',';').split(';')
        for name in othernames:
            name = name.strip()
            namestr = self._dealTitle( name )
            if namestr != "" and namestr not in ydup:
                if namestr == '微电影': continue
                if namestr == '电影': continue
                if namestr == '电视剧': continue
                if namestr == '短片': continue
                ynames.append( name )
                do_names.append( namestr )
                ydup[ namestr ] = 1
        com = set(le_names).intersection( set(do_names ) )
        if len( com ) > 0:
            return True, None

        num1 = len(xnames)
        num2 = len(ynames)

        max_len, pair = 0, None 
        for i in range(num1):
            for j in range(num2):
                words1 = le_names[i].split()
                words2 = do_names[j].split()
                com = set(words1).intersection( set( words2 ) )
                if len(com) > max_len:
                    max_len = len(com)
                    pair = ( xnames[i], ynames[j] )
        return False, pair

    def toPinYin(self, name):
        old = name
        if isinstance(old, unicode):
            old = old.encode('utf-8', 'ignore')
        if isinstance(name, str):
            name = name.decode('utf-8', 'ignore')
        if not isinstance(name, unicode):
            return old
        if len(name) <= 1:
            return old
        results = []
        for ch in name:
            if not is_unicode_hanzi(ch):
                results.append( ch.encode('utf-8', 'ignore') )
                continue
            ch = ch.encode('utf-8', 'ignore')
            ch = gpy.hanzi2pinyin_split(string=ch, split="-")
            if ch is None: continue
            if isinstance(ch, unicode):
                ch = ch.encode('utf-8', 'ignore')
            results.append(ch)
        return '-'.join( results )

def get_hanzi_num(line):
    if isinstance(line,str): line = line.decode('utf-8', 'ignore')
    num = 0
    for ch in line:
        if is_unicode_hanzi(ch):
            num += 1
    return num

if __name__ == "__main__":
    x='梦想日记之莫欺少年穷'
    y='莫欺少年穷'
    #x = '步步惊情搜狐独家完整版'
    x = '步步惊情网络完整版'
    y = '步步惊情未删减版'
    x = '海底大冒险2 The Reef 2: High Tide'
    y = '海底大冒险'
    x = '国土安全第2季（无字幕版）'
    y = '国土安全第2季'
    x = '最后大亨(原声)'
    y = '最后大亨'
    x = '法国版 极速前进 第一季'
    y = '极速前进 法国版 第二季'
    #y = '头文字D'
    #namecls = VideoNameSim( {'name':x, 'video_type':4}, {'name':y, 'video_type':4}) 
    #namecls = VideoNameSim( {'name':x, 'video_type':1}, {'name':y, 'video_type':1, 'name_ot':'奔跑吧兄弟'}) 
    namecls = VideoNameSim( {'name':x, 'video_type':4}, {'name':y, 'video_type':4, 'name_ot':''}) 
    rt =  namecls.parse()

    print x, '\t@@\t' ,y
    print '\t'.join(namecls._xlwords)
    print '\t'.join(namecls._ylwords)
    print 'sim = ', rt
    print 'part_match = ', namecls.is_part_match()
    print 'delflag(0) = ', namecls.filte_by_name(0)
    print 'delflag(NULL) = ',namecls.filte_by_name(-1)
    print 'delflag(>0) = ',namecls.filte_by_name(1)
    sys.exit()
    #f = open('delflag.tit', 'rb')
    #f = open('tyc.dic')
    #f = open('alis.txt')
    l = ['天机·富春山居图	天机 富春山居图']
    l = [x+"\t"+y]
    #for line in f:
    for line in l:
        line = line.strip()
        if line == '': continue
        line  = line.split('\t')
        if len(line) !=2 : continue
        x,y = line

        x = tradition2simple(x)
        y = tradition2simple(y)

        if x == y: continue
        if is_english_line(x): 
            continue
        if is_english_line(y): 
            continue
        if y.isdigit(): continue
        if y.endswith('部') and y.startswith('第'): continue
        if y.endswith('季') and y.startswith('第'): continue
        if y.endswith('集') and y.startswith('第'): continue
        if y.endswith('篇') and y.startswith('第'): continue
        if y.endswith('话') and y.startswith('第'): continue

        namecls = VideoNameSim( {'name':x, 'video_type':1}, {'name':y, 'video_type':1, 'name_ot':''}) 
        rt =  namecls.parse()
        u1=x.decode('utf-8', 'ignore')
        u2=y.decode('utf-8', 'ignore')
        u1= u1.replace(u2, u'').strip()
        if u1 != u'':
            u2 = u2.replace(u1, u'').strip()

        if len(u1) <= 4 and u1.endswith(u'版'): continue
        if len(u2) <= 4 and u1.endswith(u'版'): continue

        if len(x.decode('utf-8', 'ignore'))<=1: continue
        if len(y.decode('utf-8', 'ignore'))<=1: continue
        x = re.sub(' [A-Za-z][-_&,:.A-Za-z ]{10,}$', '', x)
        y = re.sub(' [A-Za-z][-_&,:.A-Za-z ]{10,}$', '', y)
        #flag = namecls.filte_by_name(0)
        if rt >= 0.99: continue
        if get_hanzi_num(x) <= 1: continue
        if get_hanzi_num(y) <= 1: continue
        x = x.lower()
        y = y.lower()

        x = x.split()
        y = y.split()
        dx, dy = {}, {}
        for w in x: dx[w] = 1
        for w in y: dy[w] = 1
        x = [w for w in x if w not in dy and not is_english_line(w) ]
        y = [w for w in y if w not in dx and not is_english_line( w) ]
        if not x: continue
        #print '\n'.join(x)
        for w in x:
            if w.isdigit(): continue
            if w.startswith('第') and w.endswith('季'):continue
            if w.startswith('第') and w.endswith('部'):continue
            if w.startswith('第') and w.endswith('篇'):continue
            if w.startswith('第') and w.endswith('话'):continue
        print '\n'.join(y)
        #for w in y:
        #    print ' '.join(x)+"\t" + w
        #print x+"\t"+y
        continue
        if flag > 0: continue
        print x, "\t@@@@\t" ,y
        print 'sim = ', rt
        #print 'part_match = ', namecls.is_part_match()
        #print 'flag(NULL) = ', namecls.filte_by_name(-1)
        #print 'flag(0) = ', namecls.filte_by_name(0)
        print 'flag(>0) = ', flag 
    
    f.close()
